package com.example.design.singletonDemo;

/**
 * 懒汉模式  线程不安全
 */
public class Singleton2 {

    private static Singleton2 singleton2;

    public Singleton2() {
    }

    public static Singleton2 getSingleton2() {
        if (singleton2 == null) {
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            singleton2 = new Singleton2();
        }
        return singleton2;
    }

    public static void main(String[] args) {
        for (int i = 0 ; i <100;i++){
            new Thread(new Runnable() {
                @Override
                public void run() {
                    Singleton2 singleton2 = Singleton2.getSingleton2();
                    System.out.println(Thread.currentThread().getName()+"----------------"+singleton2);
                }
            }).start();
        }
    }
}
